﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Perceiveit.Xml
{
    public class XSimpleFactory<T> : XFactory<T> where T : XEntity, new()
    {
        private XName _name;

        public XSimpleFactory(string name, string namespaceName)
            : this(XName.Get(name, namespaceName))
        {
        }

        public XSimpleFactory(XName name)
        {
            _name = name;
        }

        public override bool IsKnownName(XName name, out XFactory<T> factory)
        {
            if (this.IsMatchingName(this._name, name))
            {
                factory = this;
                return true;
            }
            else
            {
                factory = null;
                return false;
            }
        }

        public override bool IsKnownEntity(T entity, out XFactory<T> factory)
        {
            if (entity is T)
            {
                factory = this;
                return true;
            }
            else
            {
                factory = null;
                return false;
            }
        }

        public override T GetEntityForName(XName name)
        {
            if (this.IsMatchingName(this._name, name))
                return new T();
            else
                return null;
        }

        public override XName GetNameForEntity(T entity)
        {
            if (entity is T)
                return this._name;
            else
                return null;
        }
    }
}
